<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
			
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				if(fileId){
					ajaxArr.push({
						url:'customerOwnerAdjustment/getValue',
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posName"),mainData)
					if (_that.subConfigData["title1"]) {
						let configData = _that.subConfigData["title1"]
						let attr = {}  
						if (hasEditor('title1', 'customerCodeId', configData)) {
							let title = getEditorTitle('title1', 'customerCodeId', configData)
							attr.titleTemplate = title+':<%= customerCodeId %>'
						}
						if (hasEditor('title1', 'arrangeMode', configData)) {
							let title = getEditorTitle('title1', 'arrangeMode', configData)
							attr.subtitle1Template = title+':<%= arrangeModeName %>'
						} 
						if (hasEditor('title1', 'ownerNameId', configData)) {
							let title = getEditorTitle('title1', 'ownerNameId', configData)
							attr.subtitle2Template = title+':<%= ownerName %>'
						} 
						changeEditor('title1', 'dataStr', attr, mainData);
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[1].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value[index])
								_that.$set(_that.formdata,index,res[1].value[index])
							}
						}
						if(_that.subEditorObj["title1"]){
							let dataList = _that._.cloneDeep(res[1].value.dataList)
							_that.subEditorObj["title1"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "objStr", JSON.stringify(dataList))
							_that.$set(_that.formdata, "dataStr", JSON.stringify(dataList))
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				if(_that.edit){
					_that.om.formSave('customerOwnerAdjustment/doUpdate',_that.formdata,_that)
				}else{
					_that.om.formSave('customerOwnerAdjustment/doAdd',_that.formdata,_that)
				}
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				
			}
		}
	}
</script>

<style>
</style>
       